#include <stdlib.h>
#include <stdio.h>
#include "bst.h"

int compare(void * p1, void * p2)
{
  return *(int*)p1-*(int*)p2;
}

void printFunc(void * p,visit v, int level)
{
  if(v == preorder||v == leaf)
    {
      printf("level %d: %d\n", level, *(int*)p);
    }
}

int main()
{
  //a new tree
  pbst tree = bst_new(compare);

  //sample data
  int array[] = 
    {
      8,3,10,1,6,14,4,7,13
    };
  int i;
  int * p;
  
  for(i=9;i!=0;i--)
    {
      //insert
      p = malloc(sizeof(int));
      *p = array[9-i];
      
      bst_insert(tree, (void*)p);
    }
  
  int t1,t2,t3;
  t1=1;
  t2=5;
  t3=6;
  bst_insert(tree, &t2);
  
  //search
  printf("%d\n", *(int*)bst_search(tree, (void*)&array[6]));
  printf("%d\n", *(int*)bst_search(tree, &t1));
  printf("%d\n", *(int*)bst_search(tree, &t2));
  printf("%d\n", *(int*)bst_search(tree, &t3));
  
  bst_delete(tree, &t2);
  printf("%p\n", bst_search(tree, &t2));
  
  bst_walk(tree, printFunc);

  bst_insert(tree, malloc(sizeof(int)));

  //delete
  bst_dispose(tree, NULL);
  
  return 0;
}
